package hxy.dragon.spring.webflux.controller;

import hxy.dragon.spring.webflux.dao.mapper.UserMapper;
import hxy.dragon.spring.webflux.dao.model.UserModel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Mono;

import javax.annotation.Resource;
import java.util.Random;

/**
 * @author eric
 * @program spring-webflux
 * @description
 * @date 2020/3/21
 */
@Slf4j
@RestController
public class ThreadContoller {
    @Resource
    UserMapper userMapper;

    @GetMapping("thread")
    public Mono<String> thread(Integer j) {
        log.info("开始插入");
        // 多线程，并发
        new Thread(new UserRunnable(j), "thread").start();
        new Thread(new UserRunnable(j), "thread1").start();
        new Thread(new UserRunnable(j), "thread2").start();
        new Thread(new UserRunnable(j), "thread3").start();
        new Thread(new UserRunnable(j), "thread4").start();
        new Thread(new UserRunnable(j), "thread5").start();
        new Thread(new UserRunnable(j), "thread6").start();
        new Thread(new UserRunnable(j), "thread7").start();
        new Thread(new UserRunnable(j), "thread8").start();
        new Thread(new UserRunnable(j), "thread9").start();
        new Thread(new UserRunnable(j), "thread10").start();
        new Thread(new UserRunnable(j), "thread11").start();
        new Thread(new UserRunnable(j), "thread12").start();
        new Thread(new UserRunnable(j), "thread13").start();
        new Thread(new UserRunnable(j), "thread14").start();
        new Thread(new UserRunnable(j), "thread15").start();
        new Thread(new UserRunnable(j), "thread16").start();
        new Thread(new UserRunnable(j), "thread17").start();
        new Thread(new UserRunnable(j), "thread18").start();
        new Thread(new UserRunnable(j), "thread19").start();
        return Mono.just("开始插入");
    }

    @GetMapping("thread2")
    public Mono<String> thread2(Integer j) {
        log.info("开始插入");
        for (int i = 0; i < j; i++) {
            // 多线程，并发
            new Thread(new UserRunnable(j), "thread").start();
            new Thread(new UserRunnable(j), "thread1").start();
            new Thread(new UserRunnable(j), "thread2").start();
            new Thread(new UserRunnable(j), "thread3").start();
            new Thread(new UserRunnable(j), "thread4").start();
        }

        return Mono.just("开始插入");
    }


    class UserRunnable implements Runnable {
        int j;

        UserRunnable(int j) {
            this.j = j;
        }

        @Override
        public void run() {

            for (int i = 0; i <= j; i++) {
                UserModel userModel = new UserModel();
                userModel.setAddress("江南何彩莲");
                userModel.setName("秦时明月");
                userModel.setAge(new Random().nextInt());
                userModel.setCity("天堂");
                userModel.setFirstName("不良人");
                userModel.setLastName("李星云");
                userModel.setNumber("111111111111");
                userModel.setProvince("江南三月");
                userModel.setSchool("国子监");
                userModel.setPassword("newpass");
                userMapper.insert(userModel);
                if (j == i) {
                    log.info("{}插入完成", Thread.currentThread().getName());

                }
            }


        }
    }
}
